package com.hzhjhh.view.manageView.menus.purchase.payment;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.PaymentOrder;
import com.hzhjhh.mapper.PaymentOrderMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.purchase.PaymentOrderDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-采购-付款单弹出框-下单按钮
 * 
 * @author 12083
 *
 */
public class SubmitBt {

	private static PaymentOrderMapper paymentOrderMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(PaymentOrderDialog paymentOrderDialog) {
		JButton submit = new JButton();
		submit.setBounds(379, 376, 80, 35);
		submit.setText("下   单");
		submit.addActionListener(event -> {
			Vector<Vector<String>> rowDats = paymentOrderDialog.getRowDatas();
			int i = 0;
			List<PaymentOrder> paymentOrderList = new ArrayList<>();
			Date now = new Date();
			while (i < rowDats.size()) {
				Vector idd = rowDats.get(i);
				Integer dd = new Integer(idd.get(0).toString());

				PaymentOrder paymentOrder = new PaymentOrder();
				paymentOrder.setOrderId(dd.toString());
				paymentOrder.setSupplier(idd.get(1).toString());
				paymentOrder.setCurrency(idd.get(2).toString());
				paymentOrder.setPayType(idd.get(3).toString());
				paymentOrder.setPayAmt(new BigDecimal(idd.get(4).toString()));
				paymentOrder.setBillNum(idd.get(5).toString());
				paymentOrder.setDate(DateUtil.parse(idd.get(6).toString(), DateFormatContants.YYYY_MM_DD_HH_MM_SS));
				paymentOrder.setHandler(idd.get(7).toString());
				paymentOrder.setCreator(idd.get(8).toString());
				paymentOrder.setCreatedBy("sys");
				paymentOrder.setCreatedDate(now);
				paymentOrder.setUpdatedBy("sys");
				paymentOrder.setUpdatedDate(now);
				paymentOrder.setIsDeleted(0);
				
				paymentOrderList.add(paymentOrder);
				i++;

			}
			
			if (CollectionUtils.isNotEmpty(paymentOrderList)) {
				paymentOrderMapper.batchAdd(paymentOrderList);
			}
			
		});
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		paymentOrderMapper = sqlSession.getMapper(PaymentOrderMapper.class);
		
		return submit;
	}
}
